<!DOCTYPE html>
<html>
<body>
<script src="../../../../resources/js-test.js"></script>
<script>
description("Test that we can send empty name in FormData.append()");

self.jsTestIsAsync = true;

function shouldContain(string, substring, message) {
    if (string.indexOf(substring) >= 0)
        testPassed(message);
    else
        testFailed("the FormData containing" + substring + " failed to echo correctly");
}

function firstTest() {
    var formData = new FormData();
    formData.append('', 'empty');
    formData.append('hello', 'world');
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://127.0.0.1:8000/xmlhttprequest/resources/post-echo.cgi', true);
    xhr.onload = function() {
        shouldContain(xhr.response, 'name=""\r\n\r\nempty', 
            'the FormData of string type containing a empty name echoed correctly');
        shouldContain(xhr.response, 'name="hello"\r\n\r\nworld', 
            'the FormData of string type containing a name echoed correctly');
        secondTest();
    };
    xhr.send(formData);
}

function secondTest() {
    var formData = new FormData();
    formData.append('', new Blob(), 'empty-name.txt');
    formData.append('testFile', new Blob(), 'custom-name.txt');
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://127.0.0.1:8000/xmlhttprequest/resources/post-echo.cgi', true);
    xhr.onload = function() {
        shouldContain(xhr.response, 'name=""; filename="empty-name.txt"', 
            'the FormData of file type containing a empty name echoed correctly');
        shouldContain(xhr.response, 'name="testFile"; filename="custom-name.txt"',
            'the FormData of file type containing a name echoed correctly');
        finishJSTest();
    };
    xhr.send(formData);
}
firstTest();
</script>
</body>
</html>
